package utility.file;


import java.io.InputStream;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 加载properties
 *
 * @author adam
 * @since 2016.3.3
 */
public class PropertiesLoader implements Serializable {

    private static Map<String, String> parameters = null;

    /**
     * 获取参数结果
     */
    @SuppressWarnings("Since15")
    public static String getParameter(String key, String defaultValue) {
        if (parameters == null) {
            parameters = load("config.properties");
        }
        return parameters.getOrDefault(key, defaultValue);
    }

    /**
     * 根据路径加载配置文件
     *
     * @param filePath
     * @return
     */
    public static Map<String, String> load(String filePath) {

        Map<String, String> resultMap = new HashMap<String, String>(30);

        Properties props = new Properties();

        try {
            InputStream in = PropertiesLoader.class.getClassLoader().getResourceAsStream(filePath);
            props.load(in);
            Enumeration en = props.propertyNames();
            while (en.hasMoreElements()) {
                String key = (String) en.nextElement();
                String property = props.getProperty(key);
                resultMap.put(key, property);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        return resultMap;
    }

}